TITLE MASM Template						(main.asm)

; Description:
; 
; Revision date:

INCLUDE Irvine32.inc
.data
;001 00 0000101	0: TAD location 5
;001 00 0000110	1: TAD location 6
;000 00 0000111	2: AND location 7
;011 00 0001000	3: DCA location 8
;101 00 0001001	4: JMP location 9
;000 00 0000011	5: Data 3
;000 00 0000111	6: Data 7
;000 00 0001100	7: Data 12
;000 00 0000000	8: Data 0 (8 goes here)
;010 00 0001010	9: ISZ location 10
;111 11 1111111	10: Data MAXIMUM OVERDRIVE
;101 00 0000000	11: JMP location 0
memory WORD 0000001000000101b, 0000001000000110b, 0000000000000111b, 0000011000001000b, 0000101000001001b, 0000000000000011b, 0000000000000111b, 0000000000001100b, 0000000000000000b, 0000010000001010b, 0000111111111111b, 0000101000000000b
curAddress WORD 0h
nextInstruction DWORD 0h
accumulator WORD 0h

.code
getAddressAndStore PROC ;Gets an address at location edx, stores into curAddress
	mov ax, [edx]				;Location edx is now in ax
	and ax, 0000000001111111b	;Isolated 7 address bits
	mov curAddress, ax			;Stored!
	
	ret
getAddressAndStore ENDP

getInstructionAndPerform PROC	;Gets an instruction at location edx, checks it and performs the operation
	mov ax, [edx]				;Location edx is now in ax
	and ax, 0000111000000000b	;Isolated 3-bit instruction
	
	cmp ax, 0000000000000000b	;Check for AND
	jne notAnd					;---AND INSTRUCTIONS BEGIN HERE---
	
	mov edx, OFFSET memory		;Sets first location in virtual memory
	add dx, curAddress			;Add the address offset
	add dx, curAddress			;Twice 'cuz of word
	mov ax, [edx]				;The value in curAddress is now in ax
	and accumulator, ax			;AND the value in ax with the virtual accumulator
	mov edx, nextInstruction	;Reset edx
	jmp done					;Jump down to skip the rest of the comparisons
	
	notAnd:
	cmp ax, 0000001000000000b	;Check for TAD
	jne notTad					;---TAD INSTRUCTIONS BEGIN HERE---
	
	mov edx, OFFSET memory		;Sets first location in virtual memory
	add dx, curAddress			;Add the address offset
	add dx, curAddress			;Twice 'cuz of word
	mov ax, [edx]				;The value in curAddress is now in ax
	add accumulator, ax			;And the value in ax is now added to the virtual accumulator
	mov edx, nextInstruction	;Reset edx
	jmp done					;Jump down to skip the rest of the comparisons
	
	notTad:
	cmp ax, 0000011000000000b	;Check for DCA
	jne notDca					;---DCA INSTRUCTIONS BEGIN HERE---

	mov edx, OFFSET memory		;Sets first location in virtual memory
	add dx, curAddress			;Add the address offset
	add dx, curAddress			;Twice 'cuz of word
	mov ax, accumulator			;Put the contents of virtual accumulator into ax
	mov [edx], ax				;And deposit the virtual accumulator into the memory location specified in the address
	mov edx, nextInstruction	;Reset edx
	jmp done					;Jump down to skip the rest of the comparisons

	notDca:
	cmp ax, 0000101000000000b	;Check for JMP
	jne notJmp					;---JMP INSTRUCTIONS BEGIN HERE---
	
	mov edx, OFFSET memory		;Sets first location in virtual memory
	add dx, curAddress			;Add the address offset
	add dx, curAddress			;...and don't reset edx because we want to jump here
	dec edx						;Do this twice to compensate for adds in main proc (pretty bad practice!)
	dec edx
	jmp done					;Jump down to skip the rest of the comparisons
	
	notJmp:
	cmp ax, 0000010000000000b	;Check for ISZ
	jne notIsz					;---ISZ INSTRUCTIONS BEGIN HERE---
	
	mov edx, OFFSET memory		;Sets first location in virtual memory
	add dx, curAddress			;Add the address offset
	add dx, curAddress			;Twice 'cuz of word
	mov ax, [edx]				;The value in curAddress is now in ax
	inc ax						;The incrementing part of ISZ!
	mov [edx], ax				;Incrementing it in virtual memory
	and ax, 0000111111111111b	;Getting rid of the 4 bits that aren't in PDP-8 memory
	mov edx, nextInstruction	;Reset edx early 
	cmp ax, 0000000000000000b	;The seeing if it's 0 part of ISZ...
	jne iszNotZero				;...skip this next part if it isn't
	inc edx						;Skip an instruction if it was zero
	inc edx
	iszNotZero:
	jmp done					;Jump down to skip the rest of the comparisons

	notIsz:
	done:
	ret
getInstructionAndPerform ENDP
	
main PROC
	mov edx, OFFSET memory		;Sets first location in virtual memory
do:
	mov nextInstruction, edx
	call getAddressAndStore
	call getInstructionAndPerform
	inc edx						;Next instruction
	inc edx						;Needs to do twice 'cuz of word!
	
	jmp do

	exit
main ENDP

END main